/**
 * 添加或者修改页面
 */
var StudentsInfoDlg = {
    data: {
        id: "",
        studentName: "",
        dateOfBirth: "",
        idcard: "",
        cellPhoneNumber: "",
        homeAddress: "",
        contactPhone: "",
        contactRelation: "",
        emergencyContactPhone: "",
        emergencyContactRelation: "",
        diploma: "",
        source: "",
        collegeName: "",
        months: "",
        sex: "",
        status: "",
        specialtyId: "",
        teamId: "",
        password: "",
        salt: "",
        headerUrl: "",
        remark: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var admin = layui.admin;

    //初始化时间选择器
    laydate.render({
        elem: '#dateOfBirth'
        , type: 'datetime'
    });

    getSpecialtyType();
    //获取专业类型并展示

    function getSpecialtyType() {
        var ajax = new $ax(Feng.ctxPath + "/specialtyType/showList");
        var result = ajax.start();
        $('#specialtyId').append('<option value=""></option>');
        result.data.map(item =>{
            $('#specialtyId').append('<option value="'+item.id+'">'+item.typeName+'</option>');
        });
        form.render('select');
    }

    //普通图片上传
    upload.render({
        elem: '#picBtn'
        , url: Feng.ctxPath + '/system/upload'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#photo').attr('src', result);
            });
        }
        , done: function (res) {
            $("#headerUrl").val(res.data.fileId);
            Feng.success(res.message);
        }
        , error: function () {
            Feng.error("上传照片失败！");
        }
    });

    //随机获取a-z中4个字符
    function getAZ(){
        var arr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
        var arr2="";
        for(var i=0; i<4 ; i++){
            //随机下标0~25
            //0~1*26 0~25.x 向下取整 0~25
            var n=Math.floor(Math.random()*arr.length);
            // console.log(n,arr[n]);
            // 将每次获取的随机元素放入到arr2里
            arr2 += arr[n];//console.log(n,arr[n]);
            //解决不重复，每次获取到随机后，从原数据组删除该元素
            arr.splice(n,1);
        }
        return arr2;
    }

    //通过4个字母获取16位数字
    function getRandomNum(str){
        let num = str.split('').map(char => char.charCodeAt()).join('')
        return Math.sin(num).toString().split('.')[1].slice(0,16)
    }

    //通过手机上传，生成二维码
    $("#toTellPhonePicBtn").click(function(){
        var topic = getRandomNum(getAZ());
        var url = "http://" + document.location.hostname +"/assets/tellphoneToPhoto/index.html?token=" + topic.substring(0, 8) + Date.now() + topic.substring(8, 16);
        new QRCode(document.getElementById("qrcode"), url);
    })

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/students/addItem", function (data) {
            Feng.success("添加成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });


});